package com.future.web.sys_role.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.utils.ResultUtils;
import com.future.utils.ResultVo;
import com.future.web.sys_role.entity.RoleParam;
import com.future.web.sys_role.entity.SysRole;
import com.future.web.sys_role.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    //增
    @PostMapping
    public ResultVo add(@RequestBody SysRole sysRole) {
        if (sysRoleService.save(sysRole)){
            return ResultUtils.success("添加成功");
        }
        return ResultUtils.error("添加失败");
    }

    // 改
    @PutMapping
    public ResultVo edit(@RequestBody SysRole sysRole) {
        if (sysRoleService.updateById(sysRole)){
            return ResultUtils.success("修改成功");
        }
        return ResultUtils.error("修改失败");
    }

    // 删
    @DeleteMapping("/{roleId}")
    public ResultVo delete(@PathVariable("roleId") Long roleId) {
        if (sysRoleService.removeById(roleId)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");
    }

    // 分页查询
    @GetMapping("/list")
    public ResultVo list(RoleParam param) {
        //1.构造分页查询对象
        IPage<SysRole> page = new Page(param.getCurrentPage(),param.getPageSize());

        //2. 拼接查询条件，调用service方法查询
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<SysRole>()
                .like(StringUtils.isNotEmpty(param.getRoleName()), SysRole::getRoleName, param.getRoleName());
        IPage<SysRole> list = sysRoleService.page(page, queryWrapper);

        //3. 返回结果
        return ResultUtils.success("查询成功",list);
    }

}
